package com.ruoyi.web.controller.user;

import java.sql.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.UserOrder;
import com.ruoyi.web.domain.UserOrderDetail;
import com.ruoyi.web.service.IUserOrderDetailService;
import com.ruoyi.web.service.IUserOrderService;
import com.ruoyi.web.service.IUserSettlementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 销售单Controller
 * 
 * @author ruoyi
 * @date 2025-06-11
 */
@RestController
@RequestMapping("/admin/order")
public class UserOrderController extends BaseController
{
    @Autowired
    private IUserOrderService userOrderService;

    @Autowired
    private IUserOrderDetailService userOrderDetailService;

    @Autowired
    private IUserSettlementService userSettlementService;

    /**
     * 查询销售单列表
     */
    @PreAuthorize("@ss.hasPermi('admin:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserOrder userOrder,@RequestParam(required = false,defaultValue = "0") Integer type )
    {
        startPage();
        List<UserOrder> list = userOrderService.selectUserOrderList(userOrder);
        if(type == 1 && list.size() > 0){
            List<UserOrderDetail> detailList = userOrderDetailService.selectUserOrderDetailByOrderIds(list.stream().map(UserOrder::getId).toArray(Long[]::new));
            list.forEach(item -> {
                item.setDetailList(detailList.stream().filter(detail -> detail.getOrderId().equals(item.getId())).collect(Collectors.toList()));
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出销售单列表
     */
    @PreAuthorize("@ss.hasPermi('admin:order:export')")
    @Log(title = "销售单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserOrder userOrder)
    {
        List<UserOrder> list = userOrderService.selectUserOrderList(userOrder);
        ExcelUtil<UserOrder> util = new ExcelUtil<UserOrder>(UserOrder.class);
        util.exportExcel(response, list, "销售单数据");
    }

    /**
     * 获取销售单详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        UserOrder userOrder = userOrderService.selectUserOrderById(id);
        UserOrderDetail userOrderDetail = new UserOrderDetail();
        userOrderDetail.setOrderId(id);
        List<UserOrderDetail> userOrderDetailList = userOrderDetailService.selectUserOrderDetailList(userOrderDetail);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("userOrder", userOrder);
        ajaxResult.put("detail", userOrderDetailList);
        return ajaxResult;
    }

    /**
     * 新增销售单
     */
    @PreAuthorize("@ss.hasPermi('admin:order:add')")
    @Log(title = "销售单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserOrder userOrder)
    {
        return toAjax(userOrderService.insertUserOrder(userOrder));
    }

    /**
     * 修改销售单
     */
    @PreAuthorize("@ss.hasPermi('admin:order:edit')")
    @Log(title = "销售单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserOrder userOrder)
    {
        
        return toAjax(userOrderService.updateUserOrder(userOrder));
    }

    /**
     * 删除销售单
     */
    @PreAuthorize("@ss.hasPermi('admin:order:remove')")
    @Log(title = "销售单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int rows = userOrderService.deleteUserOrderByIds(ids);
        int rows2 = userOrderDetailService.deleteUserOrderDetailByOrderIds(ids);
        return toAjax(rows+rows2);
    }
}
